﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoopPerformance
{
    class Program
    {
        public class Point
        {
            public int X { get; set; }
            public int Y { get; set; }
        }

        public static List<Point> Points { get; set; }

        static void Main(string[] args)
        {
            Init();

            BasicLoop();

            SlowerLoop();

            FasterLoop();
        }

        public static void Init()
        {
            for (int i = 0; i < 20000000; i++)
            {
                Points.Add(new Point());
            }
        }

        private static void BasicLoop()
        {
            for (int i = 0; i < 20000000; i++)
            {
                Point pt = Points[i];
                pt.X = 1;
                pt.Y = 2;
            }
        }

        private static void SlowerLoop()
        {
            for (int i = 0; i < Points.Count; i++)
            {
                Point pt = Points[i];
                pt.X = 1;
                pt.Y = 2;
            }
        }

        private static void FasterLoop()
        {
            int totalValues = Points.Count;
            for (int i = 0; i < totalValues; i++)
            {
                Point pt = Points[i];
                pt.X = 1;
                pt.Y = 2;
            }
        }
    }
}
